﻿using DirectX.Capture;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ASCM_MulticopterApp
{
    public partial class FormSettings : Form
    {
        FormMain parentForm;
        public FormSettings(FormMain parentForm1)
        {
            parentForm = parentForm1;
            InitializeComponent();
            Load();
        }
        private Filters filters = new Filters();
        private Filter videoDevice;

        private void Load()
        {

            //load serial ports
            string[] serial_ports = System.IO.Ports.SerialPort.GetPortNames();

            comboBoxArduinoPort.DataSource = serial_ports;
            // comboBoxVideoDevice.DataSource = filters.VideoInputDevices;


            for (int c = 0; c < filters.VideoInputDevices.Count; c++)
            {
                videoDevice = filters.VideoInputDevices[c];
                comboBoxVideoDevice.Items.Add(videoDevice.Name);
            }

            IPHostEntry ip = Dns.GetHostByName(Dns.GetHostName());
            string routerIP = ip.AddressList[0].ToString();
            comboBoxPhoneIp.Items.Add(routerIP);


            comboBoxArduinoPort.Text = Properties.Settings.Default.ArduinoComPortName;
            comboBoxVideoDevice.Text = Properties.Settings.Default.VideoDevice;
            comboBoxPhoneIp.Text = Properties.Settings.Default.WPhoneIpAddress;

            numericUpDownXmin.Value = Properties.Settings.Default.AxisX_MinRange;
            numericUpDownXmax.Value = Properties.Settings.Default.AxisX_MaxRange;

            numericUpDownYmin.Value = Properties.Settings.Default.AxisY_MinRange;
            numericUpDownYmax.Value = Properties.Settings.Default.AxisY_MaxRange;

            numericUpDownThrottleMin.Value = Properties.Settings.Default.AxisZ_MinRange;
            numericUpDownThrottleMax.Value = Properties.Settings.Default.AxisZ_MaxRange;

            Dictionary<string, System.Drawing.Size> cItems = new Dictionary<string, System.Drawing.Size>();
            cItems.Add("320x240", new Size(320, 240));
            cItems.Add("640x480", new Size(640, 480));
            cItems.Add("720x480", new Size(720, 480));
            cItems.Add("1280x720", new Size(1280, 720));

            comboBoxResolution.DataSource = new BindingSource(cItems, null);
            comboBoxResolution.DisplayMember = "Key";
            comboBoxResolution.ValueMember = "Value";

 
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            Load();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.ArduinoComPortName = comboBoxArduinoPort.Text;
            Properties.Settings.Default.VideoDevice = comboBoxVideoDevice.Text;
            Properties.Settings.Default.WPhoneIpAddress = comboBoxPhoneIp.Text;

            Properties.Settings.Default.AxisX_MinRange = (int)numericUpDownXmin.Value;
            Properties.Settings.Default.AxisX_MaxRange = (int)numericUpDownXmax.Value;

            Properties.Settings.Default.AxisY_MinRange = (int)numericUpDownYmin.Value;
            Properties.Settings.Default.AxisY_MaxRange = (int)numericUpDownYmax.Value;

            Properties.Settings.Default.AxisZ_MinRange = (int)numericUpDownThrottleMin.Value;
            Properties.Settings.Default.AxisZ_MaxRange = (int)numericUpDownThrottleMax.Value;

            Size value = ((KeyValuePair<string, Size>)comboBoxResolution.SelectedItem).Value;
            Properties.Settings.Default.VideoResolution = value;
            Properties.Settings.Default.Save();

            this.Close();
            parentForm.Refresh();

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxVideoDevice_MouseClick(object sender, MouseEventArgs e)
        {
            Dictionary<string, System.Drawing.Size> test = new Dictionary<string, System.Drawing.Size>();
            //  test.Add("1", "dfdfdf");
            //   test.Add("2", "dfdfdf");
            //  test.Add("3", "dfdfdf");



        }





    }
}
